import puppeteer from 'puppeteer';
import fs from 'fs';
import path from "node:path";

import {width,height} from "./config";

export async function puppeteerTest() {
    // const browser = await puppeteer.launch();

    const browser = await puppeteer.launch({
        executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe', // 👈 指定路径
        headless: true,
    });
    const page = await browser.newPage();

    // 设置页面大小
    await page.setViewport({width, height});

    // HTML + Canvas 渲染文字
    await page.setContent(fs.readFileSync('./html/puppeteer.html').toString());

    // 获取 base64 并保存为 PNG
    const pngBase64 = await page.evaluate(() => window.pngData.split(',')[1]);
    fs.writeFileSync('./image/pixel-text.png', Buffer.from(pngBase64, 'base64'));

    await browser.close();

    return path.resolve('./image/pixel-text.png');
}
